#!/bin/bash

echo "=== AI Glass 测试框架编译脚本 ==="

# 检查必要的头文件
echo "检查编译依赖..."

# 检查gcc
if ! command -v gcc &> /dev/null; then
    echo "❌ 错误: 未找到gcc编译器"
    exit 1
fi

# 检查必要的头文件
if ! echo '#include <stdarg.h>' | gcc -E - &> /dev/null; then
    echo "❌ 错误: 未找到stdarg.h头文件"
    exit 1
fi

echo "✓ 所有依赖检查通过"

echo "开始编译AI Glass测试框架..."

# 清理旧的编译文件
echo "清理旧的编译文件"
rm -rf build
mkdir -p build

# 编译参数
CFLAGS="-Wall -Wextra -std=c99 -pthread -D_GNU_SOURCE"
INCLUDES="-Iinclude"
LDFLAGS="-pthread"

# 编译daemon程序需要的对象文件
echo "编译daemon程序..."

# 编译各个源文件
gcc $CFLAGS $INCLUDES -c src/daemon_main.c -o build/daemon_main.o
if [ $? -ne 0 ]; then
    echo "❌ daemon_main.c 编译失败"
    exit 1
fi

gcc $CFLAGS $INCLUDES -c src/daemon_service.c -o build/daemon_service.o
if [ $? -ne 0 ]; then
    echo "❌ daemon_service.c 编译失败"
    exit 1
fi

gcc $CFLAGS $INCLUDES -c src/test_framework_core.c -o build/test_framework_core.o
if [ $? -ne 0 ]; then
    echo "❌ test_framework_core.c 编译失败"
    exit 1
fi

gcc $CFLAGS $INCLUDES -c src/thread_management.c -o build/thread_management.o
if [ $? -ne 0 ]; then
    echo "❌ thread_management.c 编译失败"
    exit 1
fi

gcc $CFLAGS $INCLUDES -c src/command_handlers.c -o build/command_handlers.o
if [ $? -ne 0 ]; then
    echo "❌ command_handlers.c 编译失败"
    exit 1
fi

# 链接daemon程序
echo "链接daemon程序..."
gcc build/daemon_main.o build/daemon_service.o build/test_framework_core.o build/thread_management.o build/command_handlers.o -o build/ai_glass_test $LDFLAGS
if [ $? -ne 0 ]; then
    echo "❌ daemon程序链接失败"
    exit 1
fi
echo "✓ daemon程序编译成功: build/ai_glass_test"

# 编译client程序
echo "编译client程序..."
gcc $CFLAGS $INCLUDES -c src/client_cmd.c -o build/client_cmd.o
if [ $? -ne 0 ]; then
    echo "❌ client_cmd.c 编译失败"
    exit 1
fi

# 链接client程序
echo "链接client程序..."
gcc build/client_cmd.o -o build/ai_glass_cmd $LDFLAGS
if [ $? -ne 0 ]; then
    echo "❌ client程序链接失败"
    exit 1
fi
echo "✓ client程序编译成功: build/ai_glass_cmd"

echo "=== 编译完成 ==="
echo ""
echo "程序路径:"
echo "  后台服务: ./build/ai_glass_test"
echo "  客户端:   ./build/ai_glass_cmd"
echo ""
echo "使用方法:"
echo "  1. 启动后台服务: ./build/ai_glass_test --daemon"
echo "  2. 发送命令:     ./build/ai_glass_cmd \"AT+AIGLASSPHOTO\""
echo ""

# 运行参数检查
if [ "$1" = "run" ]; then
    echo "启动演示..."
    echo ""
    echo "=== 演示: 新架构不会接管终端 ==="
    echo ""
    echo "1. 启动后台服务..."
    ./build/ai_glass_test --daemon &
    DAEMON_PID=$!
    sleep 2
    
    if ps -p $DAEMON_PID > /dev/null 2>&1; then
        echo "✓ 后台服务启动成功 (PID: $DAEMON_PID)"
        echo ""
        echo "2. 测试: 可以正常使用Linux命令"
        echo "执行 'ls' 命令:"
        ls
        echo ""
        echo "执行 'date' 命令:"
        date
        echo ""
        echo "3. 测试: 发送AI Glass命令"
        echo "发送拍照命令..."
        ./build/ai_glass_cmd "AT+AIGLASSPHOTO"
        echo ""
        echo "发送状态查询命令..."
        ./build/ai_glass_cmd "AT+AIGLASSSTATUS"
        echo ""
        echo "4. 停止后台服务..."
        ./build/ai_glass_cmd "AT+EXIT"
        wait $DAEMON_PID 2>/dev/null
        echo "✓ 演示完成"
        echo ""
        echo "🎉 新架构完美解决了'接管终端'的问题！"
        echo "   现在您可以同时使用Linux命令和AI Glass测试功能。"
    else
        echo "❌ 后台服务启动失败"
        exit 1
    fi
fi